<?php

/**
 * an specific ad
 * @property string $productType type of product
 * @property string $manufacturer manufacturer
 */
class Job extends Ad {

    public $position = 'sale';
    public $employmentCountry = 'US';
    public $employmentState = '';
    public $payForRelocation = 0;

    /**
     * combine rules
     * @return array
     */
    public function rules() {
	return CMap::mergeArray(parent::rules(), array(
		    array('position, employmentCountry, content, contact_name, contact_email', 'required'),
		    array('payForRelocation', 'numerical', 'integerOnly' => true),
		    array('employmentCountry, employmentState', 'safe')
	));
    }

    function defaultScope() {
	return array(
	    'condition' => "`type`= " . self::TYPE_JOB
	);
    }

    /**
     * rewrite type
     */
    public function beforeValidate() {
	parent::beforeValidate();

	$this->type = self::TYPE_JOB;

	return true;
    }

    /**
     * before save action
     */
    public function afterSave() {
	parent::afterSave();

	if ($this->isNewRecord) {
	    $meta = new AdMeta();
	    $meta->key = 'position';
	    $meta->value = $this->position;
	    $this->addMeta($meta);

	    $meta = new AdMeta();
	    $meta->key = 'e_country';
	    $meta->value = $this->employmentCountry;
	    $this->addMeta($meta);

	    $meta = new AdMeta();
	    $meta->key = 'e_state';
	    $meta->value = $this->employmentState;
	    $this->addMeta($meta);

	    $meta = new AdMeta();
	    $meta->key = 'relocation';
	    $meta->value = $this->payForRelocation;
	    $this->addMeta($meta);
	} elseif ($this->scenario == self::SCENARIO_UPDATE) {

	    //update ad meta
	    $meta = AdMeta::model()->findByAttributes(array(
		'ad_id' => $this->ad_id,
		'key' => 'position'
	    ));
	    if (!$meta) {
		$meta = new AdMeta();
		$meta->key = 'position';
	    }
	    $meta->value = $this->position;
	    $meta->save();

	    unset($meta);

	    $meta = AdMeta::model()->findByAttributes(array(
		'ad_id' => $this->ad_id,
		'key' => 'e_country'
	    ));
	    if (!$meta) {
		$meta = new AdMeta();
		$meta->key = 'e_country';
	    }
	    $meta->value = $this->employmentCountry;
	    $meta->save();

	    unset($meta);

	    $meta = AdMeta::model()->findByAttributes(array(
		'ad_id' => $this->ad_id,
		'key' => 'e_state'
	    ));
	    if (!$meta) {
		$meta = new AdMeta();
		$meta->key = 'e_state';
	    }
	    $meta->value = $this->employmentState;

	    $meta->save();

	    unset($meta);

	    $meta = AdMeta::model()->findByAttributes(array(
		'ad_id' => $this->ad_id,
		'key' => 'relocation'
	    ));
	    if (!$meta) {
		$meta = new AdMeta();
		$meta->key = 'relocation';
	    }
	    $meta->value = $this->payForRelocation;
	    $meta->save();

	    unset($meta);
	}
    }

    /**
     * action after find
     */
    public function afterFind() {
	parent::afterFind();

	//render meta
	$metaData = app()->cache->get('ad_meta_data_' . $this->ad_id);
	if (!$metaData) {
	    $metaData = AdMeta::model()->findAll('ad_id = ' . $this->ad_id);
	}

	//bind value
	foreach ($metaData as $meta) {
	    switch ($meta->key) {
		case 'position':$this->position = $meta->value;
		    break;
		case 'e_country':$this->employmentCountry = $meta->value;
		    break;
		case 'e_state':$this->employmentState = $meta->value;
		    break;
		case 'relocation':$this->payForRelocation = $meta->value;
		    break;
	    }
	}
    }

    /**
     *
     * @return type
     */
    public static function getPositions() {
	return array(
	    'sales' => 'Sales',
	    'press_operator' => 'Press Operator',
	    'customer_service' => 'Customer Service',
	    'pre_press' => 'Pre-press',
	    'production_manager' => 'Production Manager',
	    'production' => 'Production',
	    'other' => 'Other'
	);
    }

    public function getPosition() {
	$positions = array(
	    'sales' => 'Sales',
	    'press_operator' => 'Press Operator',
	    'customer_service' => 'Customer Service',
	    'pre_press' => 'Pre-press',
	    'production_manager' => 'Production Manager',
	    'production' => 'Production',
	    'other' => 'Other'
	);

	return $positions[$this->position];
    }

}

